#include "psearch.h"
	       
int main( int argc, char *argv[]){

  int i;
  int n;
  int global_id;
  char hostname[32];
  double keyval;
  double *A = (double *) malloc( NE * sizeof(double) );    //local array
  int ntds = NT;                                           //number of threads


  MPI_Init(&argc,&argv); 
  MPI_Comm comm=MPI_COMM_WORLD;
  int rank;    MPI_Comm_rank( comm, &rank);
  int size;    MPI_Comm_size( comm, &size);
  int namelen; MPI_Get_processor_name(hostname,  &namelen);
  int root = 0;
  int iamroot = (rank==root);
  if(iamroot) n = (argc==2) ? atoi(argv[1]) : NE_DEFAULT;
  if(iamroot) printf("\n\tUsing %d processes and %d threads/process\n\n", size,ntds);

  MPI_Bcast((void*)&n, 1, MPI_INT, root, comm);
  n = (int)floor( (double)n/size);

  srand(rank + 1);
  for(i = 0; i < n; i++) A[i] = rank*n + (double)i ;
    keyval = rand() % (size * n) ;
  //keyval = size * n - (0.5 + rank) * n;

  printf("\tRank[%d], global size=%ld, local size=%d, host=%s\n", rank, (long)n*size, n, hostname);
  //  printf("\tGenerate local array ( range (%f, %f) and local key %f on process %i \n\n", A[0], A[n], keyval, rank);

  double tstart, tend;
  tstart = MPI_Wtime();
  global_id = mpi_bsearch(&keyval, A, n, sizeof(A[0]), userComparOperator, comm, ntds);
  tend = MPI_Wtime();

  MPI_Barrier(comm); 
  fflush(stdout);

  printf("\tKey from Rank [%d] has global_id %d and it should be %d.Ellapsed time=%2.2g secs\n", rank, global_id, (int)keyval, tend - tstart);
  MPI_Finalize();
  free(A);
  return 0;
}


